/************************************************************************
This file is part of java core libraries for the simpleRTJ virtual machine.

This file is covered by the GNU GPL with the following exception:
  As a special exception, the copyright holders of this library give you permission
  to link this library with independent modules to produce an executable, regardless
  of the license terms of these independent modules, and to copy and distribute the
  resulting executable under terms of your choice, provided that you also meet, for
  each linked independent module, the terms and conditions of the license of that
  module. An independent module is a module which is not derived from or based on
  this library. If you modify this library, you may extend this exception to your
  version of the library, but you are not obligated to do so. If you do not wish
  to do so, delete this exception statement from your version.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Created/modified by:
    RTJ Computing
***********************************************************************/
package java.io;

public class FileInputStream extends InputStream
{
    private int handle;
    int grand_total_read;

    public FileInputStream(String name) throws FileNotFoundException
    {
        this(new File(name));
    }

    public FileInputStream(File file) throws FileNotFoundException
    {
        if(!file.exists())
            throw new FileNotFoundException(file.getName());

        handle = open(file.getPath());
    }

    public int available() throws IOException
    {
        int len = getFileLength0(handle);
        if(len < 0)
            throw new IOException();

        len -= grand_total_read;
        if(len <= 0)
            return 0;

        return len;
    }

    public synchronized int skip(int num_bytes) throws IOException
    {
        if(num_bytes <= 0)
            return 0;

        if(num_bytes > available())
            num_bytes = available();

        int bytes_skipped = skip0(handle, num_bytes);
        if(bytes_skipped < 0)
            throw new IOException();

        grand_total_read += bytes_skipped;

        return bytes_skipped;
    }

    public synchronized int read() throws IOException
    {
        byte[] buf = new byte[1];

        int bytes_read = read0(handle, buf, 0, buf.length);
        if(bytes_read < 0)
            throw new IOException();

        if(bytes_read == 0)
            return -1;

        ++grand_total_read;

        return(buf[0] & 0xFF);
    }

    public int read(byte[] buf) throws IOException
    {
        return read(buf, 0, buf.length);
    }

    public synchronized int read(byte[] buf, int offset, int len) throws IOException
    {
        if(len == 0)
            return 0;

        int bytes_read = read0(handle, buf, offset, len);
        if(bytes_read < 0)
            throw new IOException();

        if(bytes_read == 0)
            return -1;

        grand_total_read += bytes_read;
        return bytes_read;
    }

    private int open(String name) throws FileNotFoundException
    {
        int rv = open0(name);
        if(rv < 0)
            throw new FileNotFoundException();

        return rv;
    }

    public synchronized void close() throws IOException
    {
        int rv = close0(handle);
        if(rv < 0)
            throw new IOException();

        handle = -1;
    }

    private native int close0(int handle);
    private native int open0(String name);
    private native int read0(int handle, byte[] buf, int offset, int len);
    private native int skip0(int handle, int num_bytes);
    private native int getFileLength0(int handle);
}

